;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Stock.UnsalableInventory", [])
        .controller("UnsalableInventoryCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "EventBus", "$filter", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, EventBus, $filter, UserContextService) {
            $scope.toolbarConfig = {};
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                    return;
                }
                $timeout(function () {
                    $scope.toolbarConfig["show" + toolbar] = true;
                });
            };
            $scope.reset = function () {
                $scope.condition = {
                    isDealer: !$scope.isZHJCH,
                };
            };

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/warehouseInventory/exporUnsaleInventorytAll")
            };

            $scope.condition = {
                isDealer: !$scope.isZHJCH,
            };
            $scope.activeFlag = [true, false];
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.markInfo = {
                curRow: -1,
            };
            $scope.dataList = [];
            $scope.dataListForAdd = [];
            $scope.tbConf = {
                fixedWidth: 80,
                scrollWidth: 1000,
                tableCol: [
                    {
                        name: "客户名称",
                        dataField: "dealerName",
                        width: 200,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "网点名称",
                        dataField: "siteName",
                        width: 120,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 120,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 160,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "库存数量",
                        dataField: "inventoryQty",
                        width: 120,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        name: "计量单位",
                        dataField: "unit",
                        width: 120,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "operate",
                        name: "操作",
                        width: 80,
                        isShow: true,
                        isFixed: true
                    }
                ]
            };
            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    scrollLeft: { left: $scope.tbConf.fixedWidth + "px" }
                };
            };
            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };
            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false, false];
                $scope.activeFlag[index] = true;
                switch (index) {
                    case 0:
                        initData();
                        getDataList();
                        break;
                    case 1:
                        break;
                    default:
                        break;
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            function initData() {
                $scope.condition = {};
                $scope.markInfo = {
                    curRow: -1,
                };
                $scope.pageInfo = {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                };
            }

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/warehouseInventory/listUnsaleInventory";
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                var param = {
                    filters: angular.copy($scope.condition),
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {

                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.detailInfo = {};

            $scope.editData = function (item) {
                $scope.detailInfo = JSON.parse(JSON.stringify(item));
                $scope.changeTab(1);
            };

            $scope.saveData = function () {
                var service = "device";
                var reqUrl = "/warehouseInventory/upateUnsaleInventory";
                ybHttp.Post(service, reqUrl, $scope.detailInfo).then(function (data) {
                    if (data.code !== 0) {
                        ybWidget.ShowMessage(data.message);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0)
                });
            };

            $scope.importData = function () {
                var param = {
                    name: "滞销库存",
                    templateReqService: "device",
                    templateReqUrl: "/warehouseInventory/downloadUnsaleInvenExcel", // 模板路径
                    importReqService: "device",
                    importReqUrl: "/warehouseInventory/importUnsaleInvenExcel", // 导入接口
                    importErrorUrl: ""
                };
                ybWidget.ImportFile(param).then(function (data) {
                    getDataList(1);
                });
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
            })();
        }])
        ;
})(window, window.angular);
